<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
abstract class ORM_Table {
    protected static $conn = 'default';
    protected $field = array();
    protected $new = true;
    protected $idField = 'id';
    
    static public function getConn() {
        return self::$conn;
    }
    
    /**
     * @param    string $name    
     * @param    array $args    
     * @return   void
     */
    public function __call($name, $args) {
        if (preg_match('/^(get|set)(\w+)/', $name, $match)) {
            $attribute = underscore($match[2]);

            if ('get' == $match[1]) {
                return isset($this->field[$attribute])?$this->field[$attribute]:null;
            } else {
                $this->field[$attribute] = $args[0];
            }
        }
    }
    
    /**
     *
     * @return int
     */
    public function getId() {
        return isset($this->field[$this->idField])?$this->field[$this->idField]:null;
    }
    
    public function save() {
        $model = get_class($this);
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $table = call_user_func(array($model, 'getTableName'), $this->field);
        if ($this->isNew()) {
            $conn->insert($table, $this->field);
            $this->setId($conn->getLastInsertId());
        } else {
            $conn->update($table, $this->field, "{$this->idField} = '".$this->getId()."'");
        }
        return true;
    }
    
    public function delete() {
        $model = get_class($this);
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        $table = call_user_func(array($model, 'getTableName'), $this->field);
        $conn->delete($table, "{$this->idField} = '".$this->getId()."'");
    }
    
    public function setNew($isNew) {
        $this->new = $isNew;
    }
    
    public function isNew() {
        return $this->new;
    }
    
    public function toArray(){
        return $this->field;
    }
    
    public function setArray($value){
        $this->file = $value;
    }
}

